package www.sharetime.weshare.activity;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import www.sharetime.weshare.R;
import www.sharetime.weshare.base.BaseOhterActivity;
import www.sharetime.weshare.event.ShareContentEvent;
import www.sharetime.weshare.utils.UIUtils;

/**
 * File Description  : 编辑发布内容
 *
 * @author : zhanggeng
 * @version : v1.0
 *          **************修订历史*************
 * @email : zhanggengdyx@gmail.com
 * @date : 2017/9/21 10:50
 */

public class EditShareActivity extends BaseOhterActivity {

    @BindView(R.id.img_share_close_action)
    ImageView imgShareCloseAction;
    @BindView(R.id.img_share_publish_action)
    ImageView imgSharePublishAction;
    @BindView(R.id.edt_share_title)
    EditText edtShareTitle;
    @BindView(R.id.edt_share_content)
    EditText edtShareContent;
    @BindView(R.id.img_expression)
    ImageView imgExpression;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_share_publish_edit;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
        View statusBarView = new View(window.getContext());
        int statusBarHeight = getStatusBarHeight(window.getContext());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, statusBarHeight);
        params.gravity = Gravity.TOP;
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundColor(Color.WHITE);
        decorViewGroup.addView(statusBarView);


        popEditWindow();
    }

    private static int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = res.getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }


    @Override
    public void initData() {
        super.initData();
    }


    @OnClick({R.id.img_share_close_action, R.id.img_share_publish_action, R.id.img_expression})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_share_close_action:
                UIUtils.closeActivity(this);
                break;
            case R.id.img_share_publish_action:
                sendShareContent();
                break;
            case R.id.img_expression:
                //TODO: 表情填充动作，目前隐藏了。
                break;
        }
    }

    private void sendShareContent() {
        if (!edtShareTitle.getText().toString().trim().equals("")  || !edtShareContent.getText().toString().trim().equals("")) {
            EventBus.getDefault().post(new ShareContentEvent(edtShareTitle.getText().toString().trim(),
                    edtShareContent.getText().toString().trim()));
        }
        UIUtils.closeActivity(this);
    }

    private void popEditWindow() {
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        WindowManager.LayoutParams p = getWindow().getAttributes();  //获取对话框当前的参数值
        p.height = (int) (d.getHeight() * 1.0);   //高度设置为屏幕的1.0
        p.width = (int) (d.getWidth() * 1.0);    //宽度设置为屏幕的0.8
        p.alpha = 0.8f;      //设置本身透明度
        p.dimAmount = 0.6f;      //设置黑暗度
        getWindow().setAttributes(p);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            UIUtils.closeActivity(this);
        }
        return super.onKeyDown(keyCode, event);
    }
}
